<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="lib/layui/css/layui.css">
    <script src="lib/layui/layui.js"></script>
    <script src="lib/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="lib/js/functionfield.js"></script>
</head>
<body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add">生成补货单</button>
        <!--<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="history">历史单据</button>-->
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="formula" id="formula">计算公式</button>
        <div class="layui-inline">
            <i class="layui-icon layui-icon-about" style="font-size: 30px; color: #1E9FFF;margin-left: 10px" id="tip"></i>
        </div>
    </div>
</script>
<fieldset class="layui-elem-field layui-field-title">
    <legend>补货管理</legend>
</fieldset>

<table class="layui-hide" id="test" lay-filter="test"></table>

</body>
<script>
    layui.use(['table','layer','form'], function () {
            var table = layui.table,
                layer = layui.layer,
                form = layui.form;
            table.render({
                elem: '#test',
                url: 'xp/complete/getCompleteList',
                id: 'completeList',
                toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
                defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],

                title: '用户数据表',
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": "", //解析提示文本
                        "count": res.code, //解析数据长度
                        "data": res.obj //解析数据列表
                    };
                },
                cols: [
                    [
                        {type: 'checkbox'},
                        {
                            field: 'goodsId',
                            title: '商品编号',
                            align: 'center'
                        }, {
                        field: 'goodsName',
                        title: '商品名称',
                        align: 'center'
                    }, {
                        field: 'specificationName',
                        title: '规格',
                        align: 'center'
                    }, {
                        field: 'haveBeenOutboundQuantity',
                        title: '销售在订量',
                        align: 'center',
                        templet: function (d) {
                            return d.haveBeenOutboundQuantity.toFixed(2)
                        }
                    }, {
                        field: 'stock',
                        title: '库存',
                        align: 'center',
                        templet: function (d) {
                            return d.stock.toFixed(2)
                        }
                    }, {
                        field: 'quantity',
                        title: '采购在订量',
                        align: 'center',
                        templet: function (d) {
                            return d.quantity.toFixed(2)
                        }
                    }, {
                        field: 'safetyStock',
                        title: '最低库存',
                        align: 'center',
                        templet: function (d) {
                            return d.safetyStock.toFixed(2)
                        }
                    }, {
                        field: 'recomQuantity',
                        title: '建议采购量',
                        align: 'center',
                        filter: true,
                        templet: function (d) {
                            return d.recomQuantity.toFixed(2)
                        }
                    }
                    ]
                ],
                page: true
            });
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中的数据
            if(obj.event=='formula'){
                var msg = "建议采购量 = 销售在订 - 库存余额\n" +
                    "- 采购在订量 + 最低库存";
                layer.tips(msg , '#formula', {
                    tips: [2, '#3595CC'],
                    area: ['auto','auto']
                });
            }else if(obj.event=='add'){
                var ids=[];
                for (var i=0;i<data.length;i++){
                    ids.push(checkStatus.data[i].id)
                }
                if(data.length === 0){
                    layer.msg('请选择一行');
                } else {
                    //console.log(ids);
                    layer.open({
                         type: 2
                        ,title: '新增补货单'
                        ,area: ['600px', '380px']
                        ,shade: 0.5
                        ,id: 'LAY_layuipro'
                        ,content: 'complement_Info.html'
                        ,success: function(layero,index){
                            var iframe = window['layui-layer-iframe'+index];
                            iframe.tes(data);
                        }
                    });
                }
            }else if(obj.event=='history'){
                layer.open({
                    type: 2
                    ,title: '补货单记录'
                    ,area: ['800px', '500px']
                    ,shade: 0.5
                    ,id: 'LAY_layuipro'
                    ,content: 'complement_history.html'
                })
            }
        });

        var tips = "销售在订量：统计该商品所有关联的已生效销货订单的订购数量；<br>" +
                                "采购在订量：统计该商品所有关联的已生效采购订单的订购数量；<br>" +
                                "最低库存：取商品管理中设置的库存预警值（总仓预警、分仓预警或按属性预警）。"

        var tip_index = 0;
        jQuery(document).on('mouseenter', '#tip', function(){
            tip_index = layer.tips(tips, '#tip', {time: 0, tips: [3, '#3595CC'],area: ['410px', '100px']});
        }).on('mouseleave', '#tip', function(){
            layer.close(tip_index);
        });


    });
    function updateStatus(id) {
        $.ajax({
            url: 'xp/complete/updateCompleteList',
            type: 'post',
            data: JSON.stringify(id),
            async: false,
            dataType: "json",
            traditional: true,
            contentType: "application/json",
            success: function (data) {
                if(data.msg=='修改成功'){
                    layui.table.reload('completeList', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , url: 'xp/complete/getCompleteList'
                        , method: 'post'
                    });
                }
            },
            complete: function(){
                layer.msg("补货单已生成",{icon: 6,time: 2000});
            },
            error: function () {
                layer.msg("后台出错，请稍后再试",{icon: 0,time: 2000})
            }
        })
    }

</script>
</html>